<?php

class Taobao {

    //获取淘宝客商品数据
    private $c;

    //实例化topSDK
    private function createTOP() {
        define('TOP_SDK_WORK_DIR', RUNTIME_PATH);
        define('TOP_SDK_DEV_MODE', false);   //开发模式
        require_cache('./Top/TopSdk.php');
        $this->c = new TopClient();
        $this->c->appkey = C('TAOBAO_APP_KEY');
        $this->c->secretKey = C('TAOBAO_SECRET_KEY');   //正式
        $this->c->format = C('TAOBAO_FORMAT');
        $this->c->gatewayUrl = 'http://gw.api.taobao.com/router/rest';
    }

    public function getItem($itemid, $nick=false) {
        $nick = !$nick ? C('TAOBAO_NICK') : $nick;
//        if (!($res = S('taobao_item_' . $itemid . '_' . $nick))) {
        $this->createTOP();
        $item = new TaobaokeItemsConvertRequest;
        $item->setFields('num_iid,click_url,pic_url,title,price,commission,commission_rate,commission_num,commission_volume');
        $item->setNick($nick);
        $item->setOuterCode('xunlitao');
        $item->setNumIids("$itemid");
        $resp = $this->c->execute($item);
        unset($this->c);
        if (!empty($resp->taobaoke_items->taobaoke_item)) {    //不能判断是否有返回值,因为是对象
            $res = (array) $resp->taobaoke_items->taobaoke_item[0];
            $res['commission_rate'] = number_format($res['commission_rate'] * 0.01, 2);
            return $res;
        } else {
            return false;
        }
    }

    /**
     *  查询宝贝列表
     * @param type $keyword
     * @param type $page
     * @param type $order
     * @return type 
     */
    public function searchItem($keyword, $page=1, $order=0, $comm=0) {
        $nick = !$nick ? C('TAOBAO_NICK') : $nick;
        $this->createTOP();
        switch ($order) {
            case 0: //折扣优先
                $order = 'commissionRate_desc';
                break;
            case 1: //人气优先
                $order = 'commissionNum_desc';
                break;
            case 2: //信用
                $order = 'credit_desc';
                break;
            case 3: //价钱
                $order = 'price_asc';
                break;
            default :
                $order = 'commissionRate_desc';
        }
        switch ($comm) {
            case 0:
                $comm = '0';
                break;
            case 1:
                $comm = '500';
                break;
            case 2:
                $comm = '1000';
                break;
            case 3:
                $comm = '2000';
                break;
            case 4:
                $comm = '4000';
                break;
            case 5:
                $comm = '5000';
                break;
            default :
                $comm = '5000';
        }
        $item = new TaobaokeItemsGetRequest;
        $item->setFields('num_iid,click_url,nick,seller_credit_score,item_location,pic_url,title,price,commission,commission_rate,commission_num,commission_volume');
        $item->setNick($nick);
        $item->setOuterCode('xunlitao');
        $item->setKeyword("$keyword");
        $item->setPageNo($page);
        $item->setPageSize(C('ITEM_PAGE_SIZE'));
        $item->setSort("$order");
        $item->setStartCommissionRate("$comm");
        $item->setEndCommissionRate('9000');
        $resp = $this->c->execute($item);
        unset($this->c);
        if (!empty($resp->taobaoke_items->taobaoke_item)) {    //不能判断是否有返回值,因为是对象
            $resp = (array) $resp->taobaoke_items->taobaoke_item;
            foreach ($resp as $key => $val) {
                $resp[$key] = (array) $val;
            }
            return $resp;
        } else {
            return false;
        }
    }

    public function getShop($shopid, $nick=false) {
        $nick = !$nick ? C('TAOBAO_NICK') : $nick;
        $this->createTOP();
        $shop = new TaobaokeShopsConvertRequest;
        $shop->setFields('user_id,shop_title,click_url,commission_rate,pic_url,num_iid');
        $shop->setSids("$shopid"); //店铺ID
        $shop->setNick($nick);
        $shop->setOuterCode('xunlitao');
        $resp = $this->c->execute($shop);
        if (!empty($resp->taobaoke_shops->taobaoke_shop)) {    //不能判断是否有返回值,因为是对象
            $res = (array) $resp->taobaoke_shops->taobaoke_shop[0];
            $res['commission_rate'] = $res['commission_rate'];
            $res['title'] = $res['shop_title'];
            unset($resp, $this->c, $shop);
            return $res;
        } else {
            return false;
        }
    }

}

?>
